﻿/*
 * by cansik / Florian
 * Datum: 29.04.2009
 * Zeit: 10:55
 */
using System;
using System.Collections.Generic;
using System.Text;

using ProviderHandler;
using System.IO;
using CML;
using System.Security.Cryptography;

namespace sEmit.Core
{
    public class LogfileManager
    {
        Configurator config;

        public string Logfile { get; set; }
        public bool Enabled { get; set; }

        public LogfileManager(string Logfile)
        {
            config = new Configurator(Logfile);
            this.Logfile = Logfile;
            Enabled = true;
        }

        public void WriteLog(Contact From, Contact To, string message)
        {
            if (Enabled)
            {
                string log = "<!$" + GetMD5Hash(DateTime.Now.ToString() + DateTime.Now.Ticks.ToString()) + " datum='" + DateTime.Now.ToString() + "' fromName='" + From.Name + "' fromNR='" + From.Number + "' toName='" + To.Name + "' toNR='" + To.Number + "' message='" + message.Replace("\r", "\\r").Replace("\n", "\\n") + "' />\n";
                File.AppendAllText(Logfile, log);
            }
        }

        private string GetMD5Hash(string TextToHash)
        {
            //Prüfen ob Daten übergeben wurden.
            if ((TextToHash == null) || (TextToHash.Length == 0))
            {
                return string.Empty;
            }

            //MD5 Hash aus dem String berechnen. Dazu muss der string in ein Byte[]
            //zerlegt werden. Danach muss das Resultat wieder zurück in ein string.
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] textToHash = Encoding.Default.GetBytes(TextToHash);
            byte[] result = md5.ComputeHash(textToHash);

            return System.BitConverter.ToString(result);
        }

        public LogEntry[] GetLogs()
        {
            LogEntry[] LogList;

            config.ConfigFile = Logfile;
            config.Read();

            LogList = new LogEntry[config.reader.ElementsList.Length];

            for (Int32 i = 0; i < config.reader.ElementsList.Length; i++)
            {
                CMLElement e = config.reader.ElementsList[i];
                LogEntry le = new LogEntry(AfterLogEdit(e.Attributes["datum"]), AfterLogEdit(e.Attributes["fromName"]), AfterLogEdit(e.Attributes["fromNR"]), AfterLogEdit(e.Attributes["toName"]), AfterLogEdit(e.Attributes["toNR"]), AfterLogEdit(e.Attributes["message"].Replace("\\r", "\r").Replace("\\n", "\n")));

                LogList[i] = le;
            }

            return LogList;
        }

        string AfterLogEdit(string text)
        {
            return text.Replace("&#39;", "'");
        }

        public struct LogEntry
        {
            public LogEntry(string datum, string fromName, string fromNR, string toName, string toNR, string message)
            {
                Datum = datum;
                FromName = fromName;
                FromNR = fromNR;
                ToName = toName;
                ToNR = toNR;
                Message = message;
            }

            public String Datum;
            public String FromName;
            public String FromNR;
            public String ToName;
            public String ToNR;
            public String Message;
        }
    }
}
